/**
 * 
 */
package ua.kiev.ntukpi.asu.ga;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;

import ua.kiev.ntukpi.asu.ga.gui.PopulationGUIUpdater;
import ua.kiev.ntukpi.asu.ga.gui.MainFrame;
import ua.kiev.ntukpi.asu.ga.impl.CanonicalAligorithm;
import ua.kiev.ntukpi.asu.ga.interfaces.Algorithm;
import ua.kiev.ntukpi.asu.ga.interfaces.Crossover;
import ua.kiev.ntukpi.asu.ga.interfaces.Fitness;
import ua.kiev.ntukpi.asu.ga.interfaces.Mutation;
import ua.kiev.ntukpi.asu.ga.interfaces.Remover;
import ua.kiev.ntukpi.asu.ga.interfaces.Selection;


/**
 * @author Alexander Galibey
 *
 */
public class EntryPoint {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Algorithm algorithm = new CanonicalAligorithm();
		final List<Individual> population = PopulationGenerator.generate(10, 100);
		Selection selection = algorithm.getSelection();
		Crossover crossover = algorithm.getCrossover();
		Mutation mutation = algorithm.getMutation();
		Remover remover = algorithm.getRemover();
		final Fitness fitness = algorithm.getFitness();
		List<Individual> sortedListBefore = new ArrayList<Individual>(population);
		Collections.sort(population, fitness.getComparator());
		Individual theWorst = sortedListBefore.get(0);
		System.out.println("The worst individal before:" + theWorst
				+ "   Fitness:" + fitness.calculate(theWorst));

		Individual theBest = sortedListBefore.get(sortedListBefore.size() - 1);
		System.out.println("The best individal before:" + theBest
				+ "   Fitness:" + fitness.calculate(theBest));
		
		MainFrame frame = new MainFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		PopulationGUIUpdater updater = new PopulationGUIUpdater(frame);
		updater.update(population);
		
		for (int i = 0; i < 5000; i++){
			
			algorithm.nextGeneration(population);
			updater.update(population);
			
		}

		List<Individual> sortedListAfter = new ArrayList<Individual>(population);
		Collections.sort(population, fitness.getComparator());
		Individual theWorst2 = sortedListAfter.get(0);
		System.out.println("The worst individal after:" + theWorst2
				+ "   Fitness:" + fitness.calculate(theWorst2));

		Individual theBest2 = sortedListAfter.get(sortedListAfter.size() - 1);
		System.out.println("The best individal after:" + theBest2
				+ "   Fitness:" + fitness.calculate(theBest2));
	
		
	}

}
